package com.qf.boot2202day03.controller;

import com.qf.qfsyspringbootstarterplus.utils.QfFileAppender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/exp")
public class ExportController {

    @Autowired
    QfFileAppender qfFileAppender;
    @Autowired
    ApplicationContext applicationContext;

    @GetMapping("/test1")
    public Map<String,Object> test1() throws IOException {

        List<String> list = Arrays.asList("luffy", "zero", "nami", "wusopu", "joba");

        String filename = UUID.randomUUID().toString();

        qfFileAppender.initWriter(filename);

        qfFileAppender.appendListToFile(list);

        qfFileAppender.close();

        Map<String,Object> map = new HashMap<>();

        map.put("code",1);

        map.put("message","导出成功");

        return map;

    }
    @GetMapping("/test2")
    public Map<String,Object> test2() throws IOException {

        QfFileAppender qfFileAppender =(QfFileAppender)applicationContext.getBean("qfFileAppender");

        List<String> list = Arrays.asList("luffy", "zero", "nami", "wusopu", "joba");

        String filename = UUID.randomUUID().toString();

        qfFileAppender.initWriter(filename);

        qfFileAppender.appendListToFile(list);

        qfFileAppender.close();

        Map<String,Object> map = new HashMap<>();

        map.put("code",1);

        map.put("message","导出成功");

        return map;

    }

}
